package com.book.service.impl;

import com.book.dao.BookMapper;
import com.book.dao.StudentMapper;
import com.book.entity.Book;
import com.book.entity.Borrow;
import com.book.entity.Student;
import com.book.service.BookService;
import com.book.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author zzw2000
 * @Date 2022年08月11日 9:42
 * @Description 描述
 */
public class BookServiceImpl implements BookService {
    @Override
    public List<Borrow> getBorrowList() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession(true)) {
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            return mapper.getBorrowList();
        }
    }

    @Override
    public void returnBook(int id, int bid) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession(false)) {
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            mapper.delBorrow(id);
            mapper.updateBookStatus(bid,false);
            sqlSession.commit();
        }
    }

    @Override
    public List<Book> getActiveBookList() {
        Set<Integer> set = new HashSet<>();
        this.getBorrowList().forEach(borrow -> set.add(borrow.getBookId()));
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession(true)) {
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            return mapper.getBookList().stream()
                    .filter(book -> !set.contains(book.getBid()))
                    .collect(Collectors.toList());
        }
    }

    @Override
    public List<Book> getBookList() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession(true)) {
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            return mapper.getBookList();
        }
    }

    @Override
    public List<Student> getStudentList() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession(true)) {
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            return mapper.getStudentList();
        }
    }

    @Override
    public void addBorrow(int sid, int bid) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession(false)) {
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            mapper.addBorrow(sid, bid);
            mapper.updateBookStatus(bid,true);
            sqlSession.commit();
        }
    }

    @Override
    public void delBook(int bid) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession(true)) {
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            mapper.delBook(bid);
        }
    }

    @Override
    public void addBook(String title, String desc, Double price) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession(true)) {
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            mapper.addBook(title, desc, price);
        }
    }

    @Override
    public Integer getBookQuantity() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession(true)) {
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            return mapper.getBookQuantity();
        }
    }

    @Override
    public Integer getStudentQuantity() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession(true)) {
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            return mapper.getStudentQuantity();
        }
    }

    @Override
    public Integer getBorrowCount() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession(true)) {
            BookMapper mapper = sqlSession.getMapper(BookMapper.class);
            return mapper.getBorrowCount();
        }
    }
}
